package com.zrwl.poscloud.xo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.ServerSyncEmployeeInfo;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.HTTPUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
//import com.zrwl.poscloud.xo.global.RequestConfig;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.ServerSyncEmployeeInfoMapper;
import com.zrwl.poscloud.xo.service.ServerSyncEmployeeInfoService;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import com.zrwl.poscloud.xo.vo.ServerSyncEmployeeInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户关注持卡人云端数据 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2022-03-30
 */
@Service
public class ServerSyncEmployeeInfoServiceImpl extends SuperServiceImpl<ServerSyncEmployeeInfoMapper, ServerSyncEmployeeInfo> implements ServerSyncEmployeeInfoService {

//    @Autowired
//    private RequestConfig requestConfig;
    @Autowired
    private ServerSyncEmployeeInfoService serverSyncEmployeeInfoService;
    @Resource
    private ServerSyncEmployeeInfoMapper serverSyncEmployeeInfoMapper;

    @Override
    public IPage<ServerSyncEmployeeInfo> getPageList(ServerSyncEmployeeInfoVO serverSyncEmployeeInfoVO) {
        QueryWrapper<ServerSyncEmployeeInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(serverSyncEmployeeInfoVO.getKeyword()) && !StringUtils.isEmpty(serverSyncEmployeeInfoVO.getKeyword().trim())) {
            queryWrapper.like("name", serverSyncEmployeeInfoVO.getKeyword().trim());
        }
        Page<ServerSyncEmployeeInfo> page = new Page<>();
        page.setCurrent(serverSyncEmployeeInfoVO.getCurrentPage());
        page.setSize(serverSyncEmployeeInfoVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<ServerSyncEmployeeInfo> pageList = serverSyncEmployeeInfoService.page(page, queryWrapper);
        List<ServerSyncEmployeeInfo> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<ServerSyncEmployeeInfo> getAllList(ServerSyncEmployeeInfoVO serverSyncEmployeeInfoVO) {
        QueryWrapper<ServerSyncEmployeeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<ServerSyncEmployeeInfo> list = serverSyncEmployeeInfoService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(ServerSyncEmployeeInfoVO serverSyncEmployeeInfoVO) {
        ServerSyncEmployeeInfo entity = new ServerSyncEmployeeInfo();
        BeanUtils.copyProperties(serverSyncEmployeeInfoVO, entity, SysConf.STATUS);
        entity.insert();
        serverSyncEmployeeInfoVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(ServerSyncEmployeeInfoVO serverSyncEmployeeInfoVO) {
        ServerSyncEmployeeInfo entity = serverSyncEmployeeInfoService.getById(serverSyncEmployeeInfoVO.getUid());
        BeanUtils.copyProperties(serverSyncEmployeeInfoVO, entity);
        entity.setUpdateTime(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(ServerSyncEmployeeInfoVO serverSyncEmployeeInfoVO) {
        ServerSyncEmployeeInfo entity = serverSyncEmployeeInfoService.getById(serverSyncEmployeeInfoVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.setUpdateTime(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<ServerSyncEmployeeInfo> getSyncEmployeeInfoList() {
        QueryWrapper<ServerSyncEmployeeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<ServerSyncEmployeeInfo> resultList = serverSyncEmployeeInfoService.list(queryWrapper);
        return resultList;
    }

    @Override
    public String addSyncRecord(String employeeUid) {//根据需要补充字段(连同手机端查询一起补充防止首次采集缺失数据)
        EmployeeVO resultVO = serverSyncEmployeeInfoMapper.getSyncEmployeeData(employeeUid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uid", resultVO.getUid());
        jsonObject.put("employeeName", resultVO.getEmployeeName());
        jsonObject.put("certCode", resultVO.getCertCode());
        jsonObject.put("employeeCode", resultVO.getEmployeeCode());
        jsonObject.put("type", resultVO.getType());
        jsonObject.put("sex", resultVO.getSex());
        jsonObject.put("deptName", resultVO.getDeptName());
        jsonObject.put("companyUid", resultVO.getCompanyUid());
        jsonObject.put("companyFullName", resultVO.getCompanyFullName());
        jsonObject.put("companyCode", resultVO.getCompanyCode());
        jsonObject.put("isVip", resultVO.getIsVip());
        ServerSyncEmployeeInfo entity = new ServerSyncEmployeeInfo();
        entity.setName("同步人员信息");
        entity.setSummary("同步人员信息");
        entity.setEmployeeUid(resultVO.getUid());
        entity.setCompanyUid(resultVO.getCompanyUid());
        entity.setJsonData(jsonObject.toJSONString());
        entity.insert();
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

}
